<?php

//$tag is the content tag, need to split it to paragraphes. the title may be in the first 3 paragraphes
//at the same time, get the pre 3 tags , which may has the title infor either

//to get the pre tags, use the $leaf_tag_hz_num array
define("TITLE_MAX_LEN",30) ;
function dump_node($e,$pre="")
{
	foreach ($e as $tag=>$val){
		if($tag != "parent"){
			echo $pre. " tag:" . $tag . "\n";
			if(is_array($val)){
				dump_node($val,$pre. " ") ;
			}else{
				echo $pre ." val:" . $val . "\n";
			}
		}
	}
}
function getChildByTagName($e,$tag,$idx)
{
	$_idx=0;
	$childIdx=0;
	foreach($e->childNodes() as $ch){
		if($ch->tag == $tag) {
			if($_idx == $idx) { return $ch ; }
			else { $_idx ++ ;}
		}
		$childIdx ++ ; 
	}
	return null;
}
function get_node_by_tag($tag)
{
	global $html;
	echo "get_content_by_tag: tag " . $tag . "\n";
	$index=0;
	$arr=explode(" ", $tag) ; //tag num tag num ; 
	array_shift($arr) ; // remove the first blank
	print_r($arr) ;
	$pret=$html;
	$cret=null;
	$_tag;
	$_idx;
	$cnt=0;
	foreach ($arr as $d){
		if( $cnt%2 == 1  ){
			$_idx=$d ;
			echo "find tag ..." . $_tag . " index " . $_idx . "for tag:" . $tag . "\n" ;
			//$cret=$pret->find($_tag,$_idx);
			$cret=getChildByTagName($pret,$_tag,$_idx);
			if(empty($cret)) {
				echo "xxxxxxcan not find any tag :" . $tag . "\n" ;
				echo "the content of the ret node:\n" ;
				//dump_node($pret);
				dump_html_tree($pret);
				die ;
			}else{
				//echo "find : content :" . toutf8($cret->outertext) . "\n" ;
			}
			$pret=$cret;
		}else{
			$_tag=$d;
		}
		$cnt ++; 
	}
	//if(preg_match("/(.*)(\d)$/",$tag,$matches)){
	//	$otag=$matches[1];
	//	$index=$matches[2];
	//}	
	//if(empty($otag)) { $otag = $tag ; }
	//echo "otag: ". $otag . "  index " . $index . "\n" ;
	//$t=$html->find($otag,$index);
	if(!empty($cret)){
		return $cret;
	}else{
		return null;
	}
}
function get_content_by_tag($tag)
{
	$node=get_node_by_tag($tag);
	if(!empty($node)){
		return toutf8($node->innertext);
	}
}
function  print_innertext($e,$path,$idx)
{
	$tag=$e->tag;
	$text=toutf8($e->plaintext);
	echo $path ."//" . $tag . "\n" . $text ;
}


?>
